package com.iflytek.cosmo.ocr.service.impl;

import com.iflytek.cosmo.ocr.common.constant.CommonConstant;
import com.iflytek.cosmo.ocr.common.util.UUIDUtil;
import com.iflytek.cosmo.ocr.dao.OcrDirectoryDao;
import com.iflytek.cosmo.ocr.dao.OcrFileDao;
import com.iflytek.cosmo.ocr.model.domain.OutputMessage;
import com.iflytek.cosmo.ocr.model.entity.OcrDirectoryEntity;
import com.iflytek.cosmo.ocr.model.entity.OcrFileEntity;
import com.iflytek.cosmo.ocr.service.DataBaseModifyService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ViviZhou
 * @create 2019-10-2019/10/25 16:10
 */
@Service
public class DataBaseModifyServiceImpl implements DataBaseModifyService {
    @Autowired
    OcrFileDao ocrFileDao;

    @Autowired
    OcrDirectoryDao ocrDirectoryDao;

    @Override
    public OutputMessage modifyDirId() {
        List<String> userIds = ocrFileDao.groupByUserId();//按用户进行分组
        if (CollectionUtils.isNotEmpty(userIds)){
            for (int i = 0; i < userIds.size(); i++){
                OcrDirectoryEntity ocrDirectoryEntity = new OcrDirectoryEntity();//文件夹实体
                ocrDirectoryEntity.setUserId(userIds.get(i));
                List<OcrDirectoryEntity> odes = ocrDirectoryDao.select(ocrDirectoryEntity);//根据userid查询某个用户文件夹表中是否有记录
                if(CollectionUtils.isEmpty(odes)){//空表示没有记录
                    //如果没有记录，表示为老用户，直接创建文件夹信息（我的文件夹和示例文件夹），然后再修改对应用户的dirId
                    Date current = new Date();
                    //插入我的文件夹
                    OcrDirectoryEntity newODE = new OcrDirectoryEntity();
                    String uuid = UUIDUtil.getUUID();
                    newODE.setId(uuid);
                    newODE.setUserId(userIds.get(i));
                    newODE.setDirName(CommonConstant.DIR_TYPE_DEFAULT_NAME);
                    newODE.setDirType(CommonConstant.DIR_TYPE_DEFAULT);
                    newODE.setCreateTime(current);
                    ocrDirectoryDao.insertSelective(newODE);

                    //修改表t_ocr_file的dir_id的数据
                    OcrFileEntity ocrFileEntity = new OcrFileEntity();
                    ocrFileEntity.setDirId(uuid);
                    ocrFileEntity.setUserId(userIds.get(i));
                    ocrFileDao.modifyDirId(ocrFileEntity);//修改某用户的dirId

                    //插入示例文件夹
                    OcrDirectoryEntity newODE1 = new OcrDirectoryEntity();
                    String uuid1 = UUIDUtil.getUUID();
                    newODE1.setId(uuid1);
                    newODE1.setUserId(userIds.get(i));
                    newODE1.setDirName(CommonConstant.DIR_TYPE_EXAMPLE_NAME);
                    newODE1.setDirType(CommonConstant.DIR_TYPE_EXAMPLE);
                    newODE1.setCreateTime(current);
                    ocrDirectoryDao.insertSelective(newODE1);
                }else if (CollectionUtils.isNotEmpty(odes) && odes.size() == 1){
                    if (!odes.get(0).getDirName().equalsIgnoreCase(CommonConstant.DIR_TYPE_DEFAULT_NAME)){//如果不是我的文件夹，则创建我的文件夹并迁移数据
                        OcrDirectoryEntity defaultDir = new OcrDirectoryEntity();
                        Date defaultDirDate = new Date();
                        String uuidDefault = UUIDUtil.getUUID();
                        defaultDir.setId(uuidDefault);
                        defaultDir.setUserId(userIds.get(i));
                        defaultDir.setDirName(CommonConstant.DIR_TYPE_DEFAULT_NAME);
                        defaultDir.setDirType(CommonConstant.DIR_TYPE_DEFAULT);
                        defaultDir.setCreateTime(defaultDirDate);
                        ocrDirectoryDao.insertSelective(defaultDir);

                        //迁移数据到我的文件夹
                        OcrFileEntity ocrFileEntity = new OcrFileEntity();
                        ocrFileEntity.setDirId(uuidDefault);
                        ocrFileEntity.setUserId(userIds.get(i));
                        ocrFileDao.modifyDirId(ocrFileEntity);//修改某用户的dirId
                    }
                    if (!odes.get(0).getDirName().equalsIgnoreCase(CommonConstant.DIR_TYPE_EXAMPLE_NAME)){//不是示例文件夹则创建示例文件夹
                        OcrDirectoryEntity example = new OcrDirectoryEntity();
                        Date exampleDate = new Date();
                        String uuidExample = UUIDUtil.getUUID();
                        example.setId(uuidExample);
                        example.setUserId(userIds.get(i));
                        example.setDirName(CommonConstant.DIR_TYPE_EXAMPLE_NAME);
                        example.setDirType(CommonConstant.DIR_TYPE_EXAMPLE);
                        example.setCreateTime(exampleDate);
                        ocrDirectoryDao.insertSelective(example);
                    }
                }
            }
            //删除文件夹表中“默认资料库”记录
            OcrDirectoryEntity defaulten = new OcrDirectoryEntity();
            defaulten.setDirName("默认资料库");
            List<OcrDirectoryEntity> odeens = ocrDirectoryDao.select(defaulten);//查询是否有默认资料库
            if(CollectionUtils.isNotEmpty(odeens)){
                ocrDirectoryDao.delete(defaulten);
            }
            return new OutputMessage(0,"","用户文件夹id修改成功!");
        }
        return new OutputMessage(1,"","无任何用户文件!");
    }
}
